using System.ComponentModel.DataAnnotations.Schema;

namespace CollabApp.Domain.ValueObjects
{
    /// <summary>
    /// 位置坐标值对象
    /// </summary>
    [ComplexType]
    public class Position
    {
        public float X { get; private set; }
        public float Y { get; private set; }

        public Position(float x, float y)
        {
            X = x;
            Y = y;
        }

        /// <summary>
        /// 计算到另一个位置的距离
        /// </summary>
        /// <param name="other">另一个位置</param>
        /// <returns>距离</returns>
        public double DistanceTo(Position other)
        {
            var dx = X - other.X;
            var dy = Y - other.Y;
            return Math.Sqrt(dx * dx + dy * dy);
        }

        /// <summary>
        /// 检查是否在指定半径内
        /// </summary>
        /// <param name="center">中心点</param>
        /// <param name="radius">半径</param>
        /// <returns>是否在范围内</returns>
        public bool IsWithinRadius(Position center, double radius)
        {
            return DistanceTo(center) <= radius;
        }

        public override bool Equals(object? obj)
        {
            if (obj is not Position other) return false;
            return Math.Abs(X - other.X) < 0.001f && Math.Abs(Y - other.Y) < 0.001f;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(X, Y);
        }

        public override string ToString()
        {
            return $"({X:F2}, {Y:F2})";
        }
    }
}